package java8.lambda;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

/**
 * 内置的四大函数式接口
 * Consumer<T>：消费型
 * void accept(T t)
 * Supplier<T>：供给型
 * void get(T t)
 * Function<T>：函数型
 * void apply(T t)
 * Predicate<T>：断言型
 * void test(T t)
 */
public class Lambda1 {

    public static void main(String[] args) {
        consumerDemo(998D, m -> System.out.println("今晚消费" + m + "元"));

        List<Double> list = supplierDemo(5, () -> Math.random());
        System.out.println(list);

        String str = functionDemo("hello", s -> s.toUpperCase());
        System.out.println(str);

        boolean b = predicateDemo("debugger", s -> s.contains("bug"));
        System.out.println(b);
    }

    /**
     * 消费型
     */
    public static void consumerDemo(double money, Consumer<Double> consumer) {
        consumer.accept(money);
    }

    /**
     * 供给型
     */
    public static List<Double> supplierDemo(int num, Supplier<Double> supplier) {
        // 产生有num个随机数的list
        ArrayList<Double> list = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            list.add(supplier.get());
        }
        return list;
    }

    /**
     * 函数型
     */
    public static String functionDemo(String s, Function<String, String> function) {
        return function.apply(s);
    }

    /**
     * 断言型
     */
    public static boolean predicateDemo(String s, Predicate<String> predicate) {
        return predicate.test(s);
    }
}
